'''
 * @ author     ：廖传港
 * @ date       ：Created in 2020/11/6 11:14
 * @ description：
 * @ modified By：
 * @ ersion     : 
 * @File        : train.py 
'''
import numpy as np
from com.lcg.version8 import trainModelByTeacher as tr
from com.lcg.version8 import Loading_pictures as lp


# 加载数据集
X, Y = lp.loaddata("D:/python/data/")
# print("Y:",Y)
# X, Y = LoadMNIST()
# print("X.shape:",X.shape)  #X.shape: (200, 28, 28),200为总数
# print("Y.shape:",Y.shape)  #Y.shape: (200, 10),200为总数，10为10个数字

# YY = np.zeros(Y.shape[0], )
# YY=Y
# print(YY)
# print("YY.shape:",YY.shape)   #YY.shape: (200,)
# print("Y.shape[0]:",Y.shape)

# for i in range(Y.shape[0]):
#     idx = np.where(Y[i] == 0.9)
#     # print("idx:",idx)
#     YY[i] =  0.1
# # print("idx[0]:", idx[0])
# # print("YY[i]:", YY[8])  # 0.2
#
# print("YY:",YY)


dnn=tr.DNN()

dnn.Add(tr.CNN2D_MultiLayer(4,4,stride=2,nFilter=10))

dnn.Add(tr.DMaxPooling2D(2,2))


dnn.Add(tr.CNN2D_MultiLayer(4,4,stride=2,nFilter=2))

dnn.Add(tr.DMaxPooling2D(2,2))


# yy=dnn.Forward(X[0])

dnn.Add(tr.DFlatten())

# yy=dnn.Forward(X[0])

dnn.Add(tr.DDense(50,'sigmoid'))

# dnn.Add(DDense(100,50,'relu'))

dnn.Add(tr.DDense(10,'relu'))


# dnn.AdjustWeightRatio(5)
# dnn.Add(DDense(100,10,'linear'))

# ratio=dnn.AdjustWeightsRatio(X,YY)


# dnn.Add(DDense(10,1,'linear'))


dnn.Compile(lossMethod='SoftmaxCrossEntropy')

# ratio=dnn.AdjustWeightsRatio(X,YY)

yy=dnn.BatchPredict(X)
print(yy)
# print("X[0:80,:]:",X[0:80,:])
# dnn.Fit(X[0:80,:], Y[0:80],100)
dnn.Fit(X, Y,100)

dnn = tr.DNN()

dnn.Add(tr.CNN2D(6, 6, stride=2, nFilter=10))

dnn.Add(tr.DMaxPooling2D(2, 2))

# yy=dnn.Forward(X[0])

dnn.Add(tr.DFlatten())

# yy=dnn.Forward(X[0])

dnn.Add(tr.DDense(80, 'sigmoid', bFixRange=True))

# dnn.Add(DDense(100,50,'relu'))

dnn.Add(tr.DDense(10, 'relu', bFixRange=True))

# dnn.AdjustWeightRatio(5)
# dnn.Add(DDense(100,10,'linear'))

# ratio=dnn.AdjustWeightsRatio(X,YY)


# dnn.Add(DDense(10,1,'linear'))


dnn.Compile(lossMethod='SoftmaxCrossEntropy')

# ratio=dnn.AdjustWeightsRatio(X,YY)

# yy=dnn.BatchPredict(X)

dnn.Fit(X[0:150, :], Y[0:150, :], 200)

# predictY：预测Y BatchPredict批预测

predictY = dnn.BatchPredict(X[150:200, ])

predictYY = np.array([np.argmax(one_hot) for one_hot in predictY])

realY = Y[150:200, ]

realYY = np.array([np.argmax(one_hot) for one_hot in realY])

from sklearn.metrics import accuracy_score

accuracy_score(predictYY, realYY)
# realy=Y[180:200,]
#
# nx=yy[0]
#
# ny=realy[0]
# loss = np.sum(- ny * np.log(nx))

# crossE=CrossEntropy()
#
# loss=crossE.loss(yy[0],realy[0])